package com.love.utils;

import java.util.Random;

public final class NumberHelper {

	/**
	 * 把字符串格式化为int型数字
	 * 
	 * @param source
	 * @param defaultValue
	 * @return
	 */
	public static int formatInt(String source, int defaultValue) {
		if (source == null) {
			return defaultValue;
		}
		try {
			return Integer.valueOf(source);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	/**
	 * 把字符串格式化为long型数字
	 * 
	 * @param source
	 * @param defaultValue
	 * @return
	 */
	public static long formatLong(String source, long defaultValue) {
		if (source == null) {
			return defaultValue;
		}
		try {
			return Long.valueOf(source);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	/**
	 * 生成随机数
	 * @param length，随机数位数
	 * @return
	 */
	public static String getRandom(int length){
		Random random = new Random();
		StringBuilder ran=new StringBuilder("");
		for(int i=0;i<length;i++){
			ran.append(random.nextInt(10));
		}
		return ran.toString();
	}
	
	public static double formatToMoney(double value){
		java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");  
		return Double.valueOf(df.format(value));
	}
	
	
}
